/*******************************************************************************
 *
 *    Copyright (c) Baina Info Tech Co. Ltd
 *
 *    Ringtone Designer
 *    
 *    FileAdapter
 *    TODO File description or class description.
 *
 *    @author: qixiao
 *    @since:  2010-11-18
 *    @version: 1.0
 *
 *****************************************************************************/

package com.tools.ringtone.adapter;

import android.content.Context;
import android.database.Cursor;
import android.provider.MediaStore;
import android.view.View;
import android.widget.SimpleCursorAdapter;
import android.widget.SimpleCursorAdapter.ViewBinder;

import com.mp3.dwonloader.ringtonemaker.musicradio.version.zl.R;
import com.tools.ringtone.data.MusicFileItem;

/**
 * FileAdapter of Ringtone Designer.
 * 
 * @author qixiao
 * 
 */
public class MusicFileAdapter extends SimpleCursorAdapter implements ViewBinder {

	public MusicFileAdapter(final Context aContext, final Cursor aCursor) {
		super(aContext, R.layout.list_music_file, aCursor, new String[] {
				MediaStore.Audio.Media._ID, MediaStore.Audio.Media.TITLE,
				MediaStore.Audio.Media.ARTIST, MediaStore.Audio.Media.ALBUM },
				new int[] { R.id.image_icon, R.id.text_title, R.id.text_artist,
						R.id.text_album, });
		init();

	}

	private void init() {
		this.setViewBinder(this);
	}

	@Override
	public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
		final String path = cursor.getString(cursor
				.getColumnIndex(MediaStore.Audio.Media.DATA));
		final String title = cursor.getString(cursor
				.getColumnIndex(MediaStore.Audio.Media.TITLE));
		final String artist = cursor.getString(cursor
				.getColumnIndex(MediaStore.Audio.Media.ARTIST));
		final String album = cursor.getString(cursor
				.getColumnIndex(MediaStore.Audio.Media.ALBUM));
		final int id = cursor.getInt(cursor
				.getColumnIndex(MediaStore.Audio.Media._ID));
		final MusicFileItem audioItem = new MusicFileItem(path);
		audioItem.setTitle(title);
		audioItem.setArtist(artist);
		audioItem.setAlbum(album);
		audioItem.setId(id);
		switch (view.getId()) {
		case R.id.image_icon:
			// 把信息记录在图标控件中
			view.setTag(audioItem);
			return true;
		}
		return false;
	}

}
